// Copyright 2019 syncd Author. All Rights Reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gofile

import (
	"io/ioutil"
	"os"
)

func CreateFile(filePath string, data []byte, perm os.FileMode) error {
	return ioutil.WriteFile(filePath, data, perm)
}

// FileExists checks whether a file exists.
func FileExists(file string) bool {
	if _, err := os.Stat(file); os.IsNotExist(err) {
		return false
	}
	return true
}

// 文件是否存在，不存递归创建文件夹
func FileExistsOrCreate(dir string) error {
	exi := FileExists(dir)
	if !exi {
		// 创建文件夹
		return os.MkdirAll(dir, os.ModePerm)
	}

	return nil
}

// 删除文件
func RemoveFile(path string) error {
	return os.Remove(path)
}

// 删除文件夹
func RemoveDir(path string) error {
	return os.RemoveAll(path)
}
